﻿assert(Icetip, "没有找到Icetip!");

local Icetip = _G.Icetip;
local Icetip_RaidTarget = CreateFrame("Frame");
Icetip.RaidTarget = Icetip_RaidTarget
local L = LibStub("AceLocale-3.0"):GetLocale("Icetip")
local raidTargetIcon

function Icetip_RaidTarget:OnEnable(event, ...)
	self.db = Icetip.db["raidtarget"];

	self:RegisterEvent("RAID_TARGET_UPDATE");
	
	if event == "RAID_TARGET_UPDATE" then
		self:RAID_TARGET_UPDATE()
	end
end

function Icetip_RaidTarget:OnDisable()
	if raidTargetIcon then
		raidTargetIcon:Hide()
	end
end

function Icetip_RaidTarget:Update()
	if not raidTargetIcon then
		raidTargetIcon = GameTooltip:CreateTexture("Icetip_RaidTargetIcon_Icon", "ARTWORK");
		raidTargetIcon:SetTexture("Interface\\TargetingFrame\\UI-RaidTargetingIcons");
		raidTargetIcon:Hide()
		raidTargetIcon:SetWidth(self.db.size)
		raidTargetIcon:SetHeight(self.db.size)
		self:Reposition()
	end

	if not GameTooltip:GetUnit() then
		raidTargetIcon:Hide()
		return
	end

	if not UnitExists("mouseover") then
		return
	end

	local index = GetRaidTargetIndex("mouseover");

	if not self.db.enable then
		self:OnDisable();
	else
		if index then
			SetRaidTargetIconTexture(raidTargetIcon, index);
			raidTargetIcon:Show()
		else
			raidTargetIcon:Hide()
		end
	end
end

function Icetip_RaidTarget:OnTooltipShow()
	Icetip_RaidTarget:Update()
end

function Icetip_RaidTarget:RAID_TARGET_UPDATE()
	self:Update()
end

function Icetip_RaidTarget:Reposition()
	if not raidTargetIcon then return end
	raidTargetIcon:SetPoint("CENTER", GameTooltip, self.db.position)
end

function Icetip_RaidTarget:SetPosition(value)
	self.db.position = value
	self:Reposition();
end

function Icetip_RaidTarget:SetSize(value)
	self.db.size = value

	if raidTargetIcon then
		raidTargetIcon:SetWidth(value)
		raidTargetIcon:SetHeight(value)
	end
end

function Icetip_RaidTarget:OnTooltipHide()
	if raidTargetIcon then
		raidTargetIcon:Hide()
	end
end

Icetip_RaidTarget:RegisterEvent("PLAYER_LOGIN");
Icetip_RaidTarget:SetScript("OnEvent", Icetip_RaidTarget.OnEnable);